<?php

class City extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'city':
	 * @var integer $local_code
	 * @var string $city_name
	 * @var string $postal_code
	 * @var string $country_initial
	 * @var integer $id
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'city';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('city_name','length','max'=>255),
			array('postal_code','length','max'=>10),
			array('local_code, city_name, postal_code, id', 'required'),
			array('local_code, id', 'numerical', 'integerOnly'=>true),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'country' => array(self::BELONGS_TO, 'Country', 'country_initial')
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'local_code' => Yii::t('database', 'Local Code'),
			'city_name' => Yii::t('database', 'City Name'),
			'postal_code' => Yii::t('database', 'Postal Code'),
			'country_initial' => Yii::t('database', 'Country Initial'),
			'id' => Yii::t('database', 'Id'),
		);
	}
	
	public function getLabel() {
		if ($this->postal_code == "" || $this->postal_code == null)
			return $this->city_name;
		else
			return $this->city_name . ' (' . $this->postal_code . ')';
	}
	
	public function primaryKey() {
		return array('id');
	}
}